<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>本地网页</title>
    <style type="text/css">
      .btn {
        display: block;
        margin: 20px auto;
        padding: 5px;
        background-color: #007aff;
        border: 0;
        color: #ffffff;
        height: 40px;
        width: 200px;
      }

      .btn-red {
        background-color: #dd524d;
      }

      .desc {
        padding: 10px;
        color: #999999;
      }
    </style>
  </head>
  <body>
    <p class="desc">web-view 组件加载本地 html 示例，仅在 App 环境下生效。点击下列按钮，跳转至其它页面。</p>
    <div class="btn-list">
      <button class="btn" type="button" data-action="navigateTo">navigateTo</button>
      <button class="btn" type="button" data-action="redirectTo">redirectTo</button>
      <button class="btn" type="button" data-action="navigateBack">navigateBack</button>
      <button class="btn" type="button" data-action="reLaunch">reLaunch</button>
      <button class="btn" type="button" data-action="switchTab">switchTab</button>
      <button class="btn" type="button" data-action="getEnv">getEnv</button>
      <!-- <button class="btn" type="button" data-action="getTheme">获取主题</button> -->
    </div>
    <p class="desc">网页向应用发送消息。注意：小程序端应用会在此页面后退时接收到消息。</p>
    <div class="btn-list">
      <button class="btn btn-red" type="button" id="postMessage">postMessage</button>
    </div>
    <p class="desc">文件选择</p>
    <div style="padding: 0 10px;">
      <p style="font-size: 14px;">&lt;input type="file" /&gt;</p>
      <input type="file" />
      <p style="font-size: 14px;">&lt;input type="file" accept="image/*" multiple /&gt;</p>
      <input type="file" accept="image/*" multiple />
      <p style="font-size: 14px;">普通input</p>
      <input placeholder="底部输入框"/>
      <br/>
      <a href="https://web-ext-storage.dcloud.net.cn/uni-app-x/pkg/hello-uniappx.apk" download>hello uni-app x apk下载（自动化测试使用）</a>
    </div>
    <!-- uni 的 SDK -->
    <script type="text/javascript" src="uni.webview.1.5.5.js"></script>
    <script type="text/javascript">
      const isWeb = location.href && /^https?:/.test(location.href)
      // document.addEventListener('UniAppJSBridgeReady', function() {
      document.querySelector('.btn-list').addEventListener('click', function(evt) {
        if(isWeb) {
          alert('web端暂不支持此功能')
          return
        }
        var target = evt.target;
        if (target.tagName === 'BUTTON') {
          var action = target.getAttribute('data-action');
          console.log(action);
          switch (action) {
            case 'switchTab':
              uni.webView.switchTab({
                url: '/pages/tabBar/API'
              });
              break;
            case 'reLaunch':
              uni.webView.reLaunch({
                url: '/pages/tabBar/component'
              });
              break;
            case 'navigateBack':
              uni.webView.navigateBack({
                delta: 1
              });
              break;
            case 'getEnv':
              uni.webView.getEnv((res) => {
                uni.postMessage({
                  data: res
                })
              });
              break;
            /* case 'getTheme':
              alert(getThemePreference())
              break; */
            default:
              uni.webView[action]({
                url: '/pages/component/button/button'
              });
              break;
          }
        }
      })
      // });
      document.querySelector("#postMessage").addEventListener('click', function() {
        if(isWeb) {
          alert('web端暂不支持此功能')
          return
        }
        uni.webView.postMessage({
          data: {
            action: 'message'
          }
        })
      })

      /* function getThemePreference() {
        if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
          return 'dark';
        } else if (window.matchMedia && window.matchMedia('(prefers-color-scheme: light)').matches) {
          return 'light';
        }
        return 'unknown';
      } */
    </script>
  </body>
</html>
